//
//  ViewController.m
//  CoreAnimation
//
//  Created by m w on 2023/3/2.
//

#import "ViewController.h"
#import "MoreAnimationViewController.h"
#import "CaseViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *v1;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Core Animation基础";
}

- (IBAction)startAnimation:(UIButton *)sender {
    NSInteger tag = sender.tag;
    switch (tag) {
        case 100:
            [self position:@"position"];
            break;
        case 101:
            [self translationRota:@"transform.rotation"];
            break;
        case 102:
            [self translationRota:@"transform.rotation.x"];
            break;
        case 103:
            [self translationRota:@"transform.rotation.y"];
            break;
        case 104:
            [self translationRota:@"transform.rotation.z"];
            break;
        case 105:
            [self translationScale:@"transform.scale"];
            break;
        case 106:
            [self translationTrans:@"transform.translation"];
            break;
        case 107:
            [self translationOpacity:@"opacity"];
            break;
        case 108:
            [self translationBgColor:@"backgroundColor"];
            break;
        case 109:
            [self translationCornerRadius:@"cornerRadius"];
            break;
        case 110:
            [self translationContents:@"contents"];
            break;
        case 111:
            [self translationBorderWidth:@"borderWidth"];
            break;
        case 112:
            [self translationShadowColor:@"shadowColor"];
            break;
        case 113:
            [self valueKeyframeAni];
            break;
        case 200:
            [self transitionAniFade];
            break;
        case 201:
            [self transitionAniMoveIn];
            break;
        case 202:
            [self transitionAniPush];
            break;
        case 203:
            [self transitionAniReveal];
            break;
        case 204:
            [self transitionAnicube];
            break;
        case 205:
            [self transitionAniPageCurll];
            break;
        case 206:
            [self transitionAnioglFlip];
            break;
        case 207:
            [self transitionAnisuckEffect];
            break;
        case 208:
            [self springAni];
            break;
        case 209:
            [self groupAni];
            break;
        case 300:
            [self.navigationController pushViewController:[MoreAnimationViewController new] animated:YES];
            break;
        default:
            break;
    }
    
}

- (void)position:(NSString *)position {
    CABasicAnimation * ani = [CABasicAnimation animationWithKeyPath:position];
    ani.toValue = [NSValue valueWithCGPoint:self.view.center];
//    ani.fillMode = kCAFillModeForwards;
    ani.duration = 1.5;
    ani.repeatCount = 1;//HUGE_VALF;
    ani.removedOnCompletion = YES;
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.v1.layer addAnimation:ani forKey:@"PostionAni"];
}


- (void)translationRota:(NSString *)translation {
    CABasicAnimation * ani = [CABasicAnimation animationWithKeyPath:translation];
    ani.toValue = @M_PI;
//    ani.fillMode = kCAFillModeForwards;
    ani.duration = 1.5;
    ani.repeatCount = 1;//HUGE_VALF;
    ani.removedOnCompletion = YES;
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.v1.layer addAnimation:ani forKey:@"TranslationAni"];
}

- (void)translationScale:(NSString *)translation {
    CABasicAnimation * ani = [CABasicAnimation animationWithKeyPath:translation];
    ani.fromValue = @1;
    ani.toValue = @0.5;
//    ani.fillMode = kCAFillModeForwards;
    ani.duration = 1.5;
    ani.repeatCount = 1;//HUGE_VALF;
    ani.removedOnCompletion = YES;
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.v1.layer addAnimation:ani forKey:@"TranslationAni"];
}

- (void)translationTrans:(NSString *)translation {
    
    CABasicAnimation * ani = [CABasicAnimation animationWithKeyPath:translation];
    ani.toValue = [NSValue valueWithCGPoint:CGPointMake(60+self.v1.frame.size.width, 0)];
    ani.duration = 1.5;
    ani.repeatCount = 1;//HUGE_VALF;
    ani.removedOnCompletion = YES;
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.v1.layer addAnimation:ani forKey:@"TranslationAni"];
}

- (void)translationOpacity:(NSString *)translation {
    
    CABasicAnimation * ani = [CABasicAnimation animationWithKeyPath:translation];
    ani.toValue = @0;
    ani.duration = 0.5;
//    ani.repeatCount = HUGE_VALF;
    ani.removedOnCompletion = YES;
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.v1.layer addAnimation:ani forKey:@"TranslationAni"];
}

- (void)translationBgColor:(NSString *)translation {
    self.v1.backgroundColor = [UIColor clearColor];
    CABasicAnimation * ani = [CABasicAnimation animationWithKeyPath:translation];

    ani.toValue = (id)[UIColor redColor].CGColor;
//    ani.fillMode = kCAFillModeForwards;
    ani.duration = 1.0;
//    ani.repeatCount = 1;
//    ani.removedOnCompletion = NO;
//    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    self.v1.backgroundColor = [UIColor orangeColor];
    [self.v1.layer addAnimation:ani forKey:@"BgColorAni"];
    
}

- (void)translationCornerRadius:(NSString *)translation {
    self.v1.layer.masksToBounds = YES;
    CABasicAnimation * ani = [CABasicAnimation animationWithKeyPath:translation];
    ani.toValue = @70;
    ani.duration = 1.0;
    ani.repeatCount = 1;
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.v1.layer addAnimation:ani forKey:@"CornerRadiusAni"];
}

- (void)translationContents:(NSString *)translation {
    
    CABasicAnimation * ani = [CABasicAnimation animationWithKeyPath:translation];
    self.v1.text = @"动画";
    ani.toValue = (id)self.v1;
//    ani.fillMode = kCAFillModeForwards;
    ani.duration = 1.0;
//    ani.repeatCount = HUGE_VALF;
    ani.removedOnCompletion = YES;
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.v1.layer addAnimation:ani forKey:@"TranslationAni"];
}

- (void)translationBorderWidth:(NSString *)translation {
    
    CABasicAnimation * ani = [CABasicAnimation animationWithKeyPath:translation];
    ani.toValue = @10;
//    ani.fillMode = kCAFillModeForwards;
    ani.duration = 1.0;
//    ani.repeatCount = HUGE_VALF;
    ani.removedOnCompletion = YES;
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.v1.layer addAnimation:ani forKey:@"TranslationAni"];
}

- (void)translationShadowColor:(NSString *)translation {
    [self.v1.layer setShadowOffset:CGSizeMake(2,2)];
    [self.v1.layer setShadowOpacity:1];
    [self.v1.layer setShadowColor:[UIColor grayColor].CGColor];
    CABasicAnimation * ani = [CABasicAnimation animationWithKeyPath:@"shadowColor"];
    ani.fromValue = (id)[UIColor yellowColor].CGColor;
    ani.toValue = (id)[UIColor redColor].CGColor;
//    ani.fillMode = kCAFillModeForwards;
    ani.duration = 1.0;
//    ani.repeatCount = HUGE_VALF;
    ani.removedOnCompletion = YES;
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.v1.layer addAnimation:ani forKey:@"TranslationAni"];
}

- (void)valueKeyframeAni {
    CAKeyframeAnimation * ani = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    ani.duration = 4.0;
    ani.removedOnCompletion = YES;
//    ani.fillMode = kCAFillModeForwards;
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(100, 200)];
    NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(600, 200)];
    NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(600, 400)];
    NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(100, 400)];
    NSValue *value5 = [NSValue valueWithCGPoint:CGPointMake(100, 200)];
    ani.values = @[value1, value2, value3, value4, value5];
//    ani.repeatCount = HUGE_VALF;
    [self.v1.layer addAnimation:ani forKey:@"PostionKeyframeValueAni"];
}

- (void)transitionAniFade {
    CATransition * ani = [CATransition animation];
    ani.type = kCATransitionFade;
    ani.subtype = kCATransitionFromLeft;
    ani.duration = 1.5;
    self.v1.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1.0];
    [self.v1.layer addAnimation:ani forKey:@"transitionAni"];
}

/*
 type的enum值如下：
 kCATransitionFade 渐变
 kCATransitionMoveIn 覆盖
 kCATransitionPush 推出
 kCATransitionReveal 揭开
 */
- (void)transitionAniMoveIn {
    CATransition * ani = [CATransition animation];
    ani.type = kCATransitionMoveIn;
    ani.subtype = kCATransitionFromLeft;
    ani.duration = 1.5;
    [self.v1.layer addAnimation:ani forKey:@"transitionAni"];
}

- (void)transitionAniPush {
    CATransition * ani = [CATransition animation];
    ani.type = kCATransitionPush;
    ani.subtype = kCATransitionFromLeft;
    ani.duration = 1.5;
    [self.v1.layer addAnimation:ani forKey:@"transitionAni"];
}

- (void)transitionAniReveal {
    CATransition * ani = [CATransition animation];
    ani.type = kCATransitionReveal;
    ani.subtype = kCATransitionFromLeft;
    ani.duration = 1.5;
    [self.v1.layer addAnimation:ani forKey:@"transitionAni"];
}

- (void)transitionAniPageCurll {
    CATransition * ani = [CATransition animation];
    ani.type = @"pageCurl";
    ani.subtype = kCATransitionFromLeft;
    ani.duration = 1.5;
    [self.v1.layer addAnimation:ani forKey:@"transitionAni"];
}

- (void)transitionAnicube {
    CATransition * ani = [CATransition animation];
    ani.type = @"cube";
    ani.subtype = kCATransitionFromLeft;
    ani.duration = 1.5;
    [self.v1.layer addAnimation:ani forKey:@"transitionAni"];
}

- (void)transitionAnisuckEffect {
    CATransition * ani = [CATransition animation];
    ani.type = @"pageUnCurl";
    ani.subtype = kCATransitionFromLeft;
    ani.duration = 1.5;
    [self.v1.layer addAnimation:ani forKey:@"transitionAni"];
}

- (void)transitionAnioglFlip {
    CATransition * ani = [CATransition animation];
    ani.type = @"oglFlip";
    ani.subtype = kCATransitionFromLeft;
    ani.duration = 1.5;
    [self.v1.layer addAnimation:ani forKey:@"transitionAni"];
}

- (void)springAni {
    CASpringAnimation * ani = [CASpringAnimation animationWithKeyPath:@"transform.scale"];
    ani.mass = 10.0; //质量，影响图层运动时的弹簧惯性，质量越大，弹簧拉伸和压缩的幅度越大
    ani.stiffness = 5000; //刚度系数(劲度系数/弹性系数)，刚度系数越大，形变产生的力就越大，运动越快
    ani.damping = 100.0;//阻尼系数，阻止弹簧伸缩的系数，阻尼系数越大，停止越快
    ani.initialVelocity = 5.f;//初始速率，动画视图的初始速度大小;速率为正数时，速度方向与运动方向一致，速率为负数时，速度方向与运动方向相反
    ani.duration = ani.settlingDuration;
    ani.toValue = @(1.5);
    ani.removedOnCompletion = YES;
//    ani.fillMode = kCAFillModeForwards;
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.v1.layer addAnimation:ani forKey:@"boundsAni"];
}

- (void)groupAni {
    CABasicAnimation * posAni = [CABasicAnimation animationWithKeyPath:@"position"];
    posAni.toValue = [NSValue valueWithCGPoint:self.view.center];
    
    CABasicAnimation * opcAni = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opcAni.toValue = [NSNumber numberWithFloat:0.0];
    
    CABasicAnimation * bodAni = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    bodAni.toValue = @(0.0);
    
    CAAnimationGroup * groupAni = [CAAnimationGroup animation];
    groupAni.animations = @[posAni, opcAni, bodAni];
    groupAni.duration = 1.0;
//    groupAni.fillMode = kCAFillModeForwards;
    groupAni.removedOnCompletion = YES;
    groupAni.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.v1.layer addAnimation:groupAni forKey:@"groupAni"];
}
@end
